using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace Masood.SharePoint.ProcessingTimerJob.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("953ad2d1-2cea-4ada-bb6b-8ffbaffdde89")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;

            // Remove job if it exists.
            DeleteJobAndSettings(webApplication, Constants.JobName);

            // Create the job.
            ProcessingTimerJob job = new ProcessingTimerJob(webApplication, Constants.JobName);
            
            // Create the schedule so that the job runs daily, sometime between 
            // midnight and 4 A.M.
            SPDailySchedule schedule = new SPDailySchedule();
            schedule.BeginHour = 0;
            schedule.BeginMinute = 0;
            schedule.BeginSecond = 0;
            schedule.EndHour = 3;
            schedule.EndMinute = 59;
            schedule.EndSecond = 59;
            job.Schedule = schedule;
            job.Update();
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
            DeleteJobAndSettings(webApplication, Constants.JobName);
        }

        private void DeleteJobAndSettings(SPWebApplication webApplication, string jobName)
        {
            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                if (job.Name == jobName)
                {
                    job.Delete();
                    break;
                }
            }
        }

        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
